package cameragps;

import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.location.AddressInfo;
import javax.microedition.location.Criteria;
import javax.microedition.location.Landmark;
import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationListener;
import javax.microedition.location.LocationProvider;
import javax.microedition.location.QualifiedCoordinates;

import net.rim.device.api.lbs.Locator;
import net.rim.device.api.lbs.LocatorException;
import net.rim.device.api.lbs.MapServiceException;
import net.rim.device.api.ui.component.Dialog;

public class LocationTracker implements LocationListener {

	protected static LocationProvider m_provider;
	protected static Location m_location;
	protected static double m_Logitude;
	protected static double m_Latitude;
	protected static String m_locationAddress = "";

	private static Criteria m_criteria;

	public void locationUpdated(LocationProvider provider, Location location) {
		if (location != null && location.isValid()) {
			QualifiedCoordinates coordinates = location
					.getQualifiedCoordinates();
			m_Logitude = coordinates.getLongitude();
			m_Latitude = coordinates.getLatitude();

			getLocationAddress(m_Latitude, m_Logitude);

		} else {
			Dialog.alert("Location is invalid");
		}
	}

	public void providerStateChanged(LocationProvider provider, int newState) {

	}

	protected static void getLocationAddress(final double latitude,
			final double logitude) {
		Timer timer = new Timer();
		try {

			System.out.println("______________[GET LOACATION]");

			AddressInfo addrInfo = null;
			timer.schedule(new TimerTask() {

				public void run() {

					try {
						Locator.cancel();
					} catch (Exception e) {
					}
				}
			}, 5000);

			Landmark[] results = Locator.reverseGeocode(
					(int) latitude * 100000, (int) logitude * 100000,
					Locator.ADDRESS);

			if (results != null && results.length > 0)
				addrInfo = results[0].getAddressInfo();
			m_locationAddress = addrInfo.getField(AddressInfo.CITY);

			String s = addrInfo.getField(AddressInfo.STATE);
			if (s != null && !s.equals(""))
				m_locationAddress = m_locationAddress.concat(", " + s);

			if (m_locationAddress == null)
				m_locationAddress = "";
			System.out.println(">>>>>> : " + m_locationAddress);

		} catch (LocatorException e) {
			// locationAddress = "";
			System.out.println("____________UNABLE TO GET LOCATION NAME! " + e
					+ e.getMessage());
			e.printStackTrace();
		} catch (MapServiceException e) {
			// locationAddress = "";
			System.out.println("____________PROBLEMC GETTING MAPSERVICE! " + e
					+ e.getMessage());
			e.printStackTrace();
		} catch (NullPointerException e) {
			// locationAddress = "";
			System.out.println("____________PROBLEM GETTING LOCATION! " + e
					+ e.getMessage());
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// locationAddress = "";
			System.out.println("____________INTENRNET IS NOT REACHANBLE! " + e
					+ e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			// locationAddress = "";
			System.out.println("____________ERROR GETTING LOCATION! " + e
					+ e.getMessage());
			e.printStackTrace();
		}
		timer.cancel();
		System.out.println("______________[GET LOACATION] END");

		// return locationAddress;

	}

	protected static boolean getSinglePoint() {
		try {

			System.out.println("______________[GET SINGLE POINT]");

			resetProvider();
			setupCriteria();

			createLocationProvider();

			LocationTracker.m_location = LocationTracker.m_provider.getLocation(10);
		} catch (LocationException e) {
			//helloBerry.errorDialog("No GPS Signal: Timed out");
			m_Logitude = 0;
			m_Latitude = 0;
			m_locationAddress = "";
			return true;
		} catch (InterruptedException e) {
			helloBerry.errorDialog("No GPS Signal was detected");
			return false;
		} catch (NullPointerException e) {
			helloBerry.errorDialog("Is your GPS turned on? " + e);
			return false;
		} catch (Exception e) {
			helloBerry.errorDialog("Some Problem getting location! " + e);
			return false;
		}

		if (LocationTracker.m_location != null
				&& LocationTracker.m_location.isValid()) {
			QualifiedCoordinates coordinates = LocationTracker.m_location
					.getQualifiedCoordinates();
			LocationTracker.m_Logitude = coordinates.getLongitude();
			LocationTracker.m_Latitude = coordinates.getLatitude();

			LocationTracker.getLocationAddress(LocationTracker.m_Latitude,
					LocationTracker.m_Logitude);

			System.out.println("______________[GET SINGLE POINT] END");

		}
		return true;
	}

	private static void createLocationProvider() {
		try {
			LocationTracker.m_provider = LocationProvider
					.getInstance(m_criteria);
		} catch (LocationException e) {
			Dialog.alert("LocationException:" + e.getMessage());
		}
	}

	private static void resetProvider() {
		if (LocationTracker.m_provider != null) {
			LocationTracker.m_provider.setLocationListener(null, 0, 0, 0);
			LocationTracker.m_provider.reset();
			LocationTracker.m_provider = null;
		}
	}

	private static void setupCriteria() {
		m_criteria = new Criteria();
		m_criteria.setCostAllowed(false);
	}
}
